using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using DAL;

namespace Helpers
{
    public class PositionHelper<TItem>
        where TItem : DataManagerRow, IRowPosition, new()
    {
        private string ParentName;
        private DataManagerRow Parent;
        public PositionHelper()
        {
            Initialize(null, null);
        }

        public PositionHelper(string parentName, DataManagerRow parent)
        {
            Initialize(parentName, parent);
        }

        private void Initialize(string parentName, DataManagerRow parent)
        {
            ParentName = parentName;
            Parent = parent;
        }


        public void MoveItem(DataManager<TItem> Instance, TItem Item, Direction dir)
        {
            if (Item != null)
            {
                // getting the sibling Category
                TItem sibling = GetSiblingCategory(Instance, Item, dir);
                if (sibling != null)
                {
                    // switching the order Item and sibling Categories
                    long tempPosition = sibling.Position;
                    sibling.Position = Item.Position;
                    Item.Position = tempPosition;

                    // user must be an administrator to add a Category
                    User curUser = Managers.UserLoginManager.CurrentUser;
                    if (curUser != null && curUser.HasPermissions(User.UserStatus.Administrator))
                    {
                        Instance.Update(sibling);
                        Instance.Update(Item);
                    }
                    else
                    {
                        throw new Exception("Current user doesn't have enough permissions to add a Category.");
                    }
                }
            }
        }

        private TItem GetSiblingCategory(DataManager<TItem> Instance, TItem Item, Direction dir)
        {
            // Items with a "larger" position appear lowerr in a list of Categories,
            // so if you want to move a Category up in the list, you need to find a 
            // smaller position. Likewise, if you want to move a Category down, you
            // need a larger position.
            DataManagerQuery query = new DataManagerQuery();
            if (!String.IsNullOrEmpty(ParentName))
            {
                if (Parent != null)
                {
                    query.AddParam(ParentName, Parent.ID);
                }
                else
                {
                    query.AddParam(ParentName, null);
                }
            }

            switch (dir)
            {
                case Direction.Up:
                    query.AddParam("Position", Comparison.LessThan, Item.Position);
                    query.AddSort("Position", Sort.Desc);
                    break;
                default:
                    query.AddParam("Position", Comparison.GreaterThan, Item.Position);
                    query.AddSort("Position", Sort.Asc);
                    break;
            }

            TItem sibling;
            List<TItem> results = Instance.GetItemsByPage(query, 1, 1, true).Items; // geting the first item on the first page
            if (results.Count > 0)
            {
                sibling = results[0];
            }
            else
            {
                sibling = null;
            }

            return sibling;
        }
    }
}